import { Injectable } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd';
import { SERVERURL } from '../../config';
import { RequestClientService } from '../request-client.service';
import { ModelApi } from './model.api';
import { ModelService } from './model.service';

export class AttrOfClass {
  public id: number;
  public key: string;
  public title: string;
  public direction: string;
  public catogeryName: string;
  public constructor(param) {
    (param.id !== undefined) && (this.id = param.id);
    (param.key !== undefined) && (this.key = param.key);
    (param.title !== undefined) && (this.title = param.title);
    (param.direction !== undefined) && (this.direction = param.direction);
    (param.catogeryName !== undefined) && (this.catogeryName = param.catogeryName);
  }
}

export class AttrOfUUID {
  public id: number;
  public uuid: string;
  public name: string;
  public desValue: string;
  public consValue: string;
  public isCopy = '0';
  public constructor(param) {
    (param.id !== undefined) && (this.id = param.id);
    (param.uuid !== undefined) && (this.uuid = param.uuid);
    (param.name !== undefined) && (this.name = param.name);
    (param.desValue !== undefined) && (this.desValue = param.desValue);
    (param.consValue !== undefined) && (this.consValue = param.consValue);
    (param.isCopy !== undefined) && (this.isCopy = param.isCopy);
  }

  public toJson() {
    return {
      name: this.name,
      val: this.consValue,
      isCopy: this.isCopy
    };
  }
}

interface ClassName {
  id: number;
  name: string;
}

@Injectable()
export class ModelAttrService {
  public classAttrTlpView = false;
  public allClassName: ClassName[] = [];
  public attrCompareView = false;
  public attrOfUUID: AttrOfUUID[] = [];
  public currentUUID: string;
  public currentCatogeryName = '';
  public currentClassAttrId = 0;
  // 选中构件所有属性列表
  public classWithAttrList: any = [];
  public selectAttrListView = false;
  public constructor(
    private requestServ: RequestClientService,
    private modelApi: ModelApi,
    private modelService: ModelService,
    private confirmServ: NzModalService
  ) { }

  public addClassTemplate() {


  }

  /**
   * 获取选中类别的模板框
   */
  public getAttrOfClass(className?: string) {
    let getAttrOfClass = new Set();
    if (className !== '') {
      getAttrOfClass = this.modelApi.getAttrOfClass(className);
      let attrOfClass = [];
      getAttrOfClass.forEach((val, key) => {
        attrOfClass.push(new AttrOfClass({
          key,
          title: key,
          direction: 'left'
        }));
      });

      return attrOfClass;
    }
  }

  public addClassTemplateService(param): Promise<boolean> {
    return this.requestServ.post(SERVERURL + 'api/component-by-name/tpl/add', param).then((res) => {
      if (res.code == 1) {
        this.confirmServ.success({
          nzTitle: '保存成功'
        });
      } else {
        this.confirmServ.error({
          nzTitle: '保存失败'
        });
      }
      return true;
    });
  }

  public updateClassTemplateService(param): Promise<boolean> {
    return this.requestServ.post(SERVERURL + 'api/component-by-name/tpl/update', param).then((res) => {
      if (res.code == 1) {
        this.confirmServ.success({
          nzTitle: '保存成功'
        });
      } else {
        this.confirmServ.error({
          nzTitle: '保存失败'
        });
      }
      return true;
    });
  }

  // 获取类对应的属性模板
  public getClassTemplateService(data: {
    projectId: number;
    catogeryName: string;
  }): Promise<{ id: number; data: string[]; totalVal: number }> {
    let param = {
      projectId: data.projectId,
      catogeryName: data.catogeryName
    };
    return this.requestServ.get(SERVERURL + 'api/component-by-name/tpl/getList', param).then((res) => {
      if (res.code === 1) {
        if (res.success && res.items) {
          return {
            id: res.item.id,
            data: JSON.parse(res.items),
            totalVal: 0
          };
        } else {
          return null;
        }
      } else {
        return null;
      }
    });
  }

  // 类属性---删除属性模板
  public deleteClassTemplateService(ids: number[]): Promise<boolean> {
    return this.requestServ.post(SERVERURL + 'api/component-by-name/tpl/deletes', { ids: JSON.stringify(ids) }).then((res) => {
      // console.log(res);
      if (res.code === 1) {
        return true;
      } else {
        return false;
      }
    });
  }

  // 类属性---添加属性
  public addClassAttrsServices(param: { projectId: number; uuid: string; catogeryName: string; attrDatas: AttrOfUUID[] }): Promise<boolean> {
    let data = {
      projectId: param.projectId,
      uuid: param.uuid,
      catogeryName: param.catogeryName,
      attrDatas: JSON.stringify(param.attrDatas)
    };
    return this.requestServ.post(SERVERURL + 'api/component-by-name/add', data).then((res) => {
      if (res.code === 1) {
        return true;
      } else {
        return false;
      }
    });
  }

  // 类属性---获取构件属性
  public getUUidClassAttrs() {
    let catogeryName = this.currentCatogeryName;
    let projectId = localStorage.getItem('TenantId');
    let uuid = this.currentUUID;
    let param = {
      catogeryName,
      projectId,
      uuid
    };
    let attrOfUUID = this.attrOfUUID;
    this.getClassAttrsServices(param).then((res) => {
      if (res) {
        let attrDatas = JSON.parse(res.attrDatas);
        attrDatas.forEach((element) => {
          let findIndex = attrOfUUID.findIndex((val) => {
            return element.name == val.name;
          });
          if (findIndex !== -1) {
            attrOfUUID[findIndex].isCopy = element.isCopy == 0 ? '0' : '1';
            attrOfUUID[findIndex].consValue = element.val;
          }
        });
      }
    });
  }

  public getClassAttrsServices(param: {
    catogeryName: string;
    uuid: string;
    projectId: string;
  }) {
    return this.requestServ.get(SERVERURL + 'api/component-by-name/find', param).then((res) => {
      // //console.log(res)
      if (res.code === 1 && res.item) {
        return res.item;
      } else {
        return false;
      }
    });
  }

  public updateClassAttrsServices(param: {
    id: number;
    projectId?: number;
    uuid?: string;
    catogeryName?: string;
    attrDatas?: string;
  }): Promise<boolean> {
    return this.requestServ.post(SERVERURL + 'api/component-by-name/update', param).then((res) => {
      if (res.code === 1) {
        return true;
      } else {
        return false;
      }
    });
  }

  public async uuidGetAttrByTpl(uuid, catogeryName, tpls): Promise<Array<{ name: string; desValue: string }>> {
    let designAttrs = this.modelApi.getproperty(uuid);
    let attrOfUUID = [];
    if (tpls) {
      tpls.data.forEach((element) => {
        attrOfUUID.push(new AttrOfUUID({
          name: element,
          desValue: designAttrs[element] ? designAttrs[element] : ''
        }));
      });
    }

    return attrOfUUID;

  }

}
